/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Loaisp;
import util.HibernateUtil;

/**
 *
 * @author NguyenMinhTan
 */
public class LoaiSanPhamDao {
     public static List<Loaisp> SelectAllLoaisp() {
        List<Loaisp> lsLoaisp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Loaisp lsp where lsp.tinhtrang=:tinhTrang order by lsp.id asc");
        query.setInteger("tinhTrang", 1);
        lsLoaisp = query.list();
        session.close();
        return lsLoaisp;
    }

    public static Loaisp layThongTinLoaisp(int malsp) {
        Loaisp lsp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        lsp = (Loaisp) session.get(Loaisp.class, malsp);
        return lsp;
    }

    public static boolean InsertLoaisp(Loaisp lsp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (LoaiSanPhamDao.layThongTinLoaisp(lsp.getIdloai()) != null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            session.save(lsp);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            kq = false;
        } finally {
            session.close();
        }

        return kq;
    }

    public static void DeleteLoaisp(Loaisp lsp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        lsp.setTinhtrang(0);
        session.update(lsp);
        Transaction tx = session.beginTransaction();
        tx.commit();
        session.close();

    }

    public static void UpdateLoaisp(Loaisp lsp) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        transaction = (Transaction) session.beginTransaction();
        session.update(lsp);
        transaction.commit();
        session.close();

    }
}
